﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;

namespace GoodMan.Portal.Biz
{
    public class ProductCategoryBiz
    {
        public static ItemListBox<ProductCategory> GetAllProductCategory()
        {
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.ProductCategory
                            join di in ctx.ProductCategory
                             on c.Id equals di.ParentId into dt
                            from d in dt.DefaultIfEmpty()
                            where c.ParentId == 0 
                            group new { c, d } by c.Id into g
                            select g)
                           .ToList()
                           .Select(e => new ProductCategory().Set(e.FirstOrDefault().c)
                               .LoadProductCategorys(e.Where(i => i.d != null).Select(i => new ProductCategory().Set(i.d)).ToList()))
                           .ToList();
                var list = new ItemListBox<ProductCategory>(data);
                return list;
            }

        }

        public static List<ProductCategory> GetProductCategoryList()
        {
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.ProductCategory
                            join di in ctx.ProductCategory
                             on c.Id equals di.ParentId into dt
                            from d in dt.DefaultIfEmpty()
                            where c.ParentId == 0
                            group new { c, d } by c.Id into g
                            select g)
                           .ToList()
                           .Select(e => new ProductCategory().Set(e.FirstOrDefault().c)
                               .LoadProductCategorys(e.Where(i => i.d != null).Select(i => new ProductCategory().Set(i.d)).ToList()))
                           .ToList();

                return data;
            }

        }

        public static ItemListBox<ProductCategory> GetProductCategorysByLevel(int level = 0)
        {
            var data = new ProductCategory().FindAll(c => c.ParentId == level&&c.IsActive==true);

            var list = new ItemListBox<ProductCategory>(data);
            return list;
        }

        public static bool SaveProductCategory(int productCategoryId,string productCategoryName, int parentID, bool isActive)
        {
            try
            {
                if (productCategoryId > 0)
                {
                    var productCategory = GetProductCategoryById(productCategoryId);
                    productCategory.CategoryName = productCategoryName;
                    productCategory.ParentId = parentID;
                    return productCategory.PostModify()!=null;
                }
                else
                {
                    ProductCategory productCategory =
                        new ProductCategory()
                        {
                            CategoryName = productCategoryName,
                            ParentId = parentID,
                            IsActive = isActive

                        };
                    return productCategory.PostAdd() != null;
                }
            }
            catch
            {
                return false;
            }
        }

        public static ProductCategory GetProductCategoryById(int productCategoryId)
        {
            return new ProductCategory().Find(productCategoryId.ToString());
        }
        public static bool DeleteProductCategory(int productCategoryId)
        {
            try
            {
                var productCategory = new ProductCategory().Find(productCategoryId.ToString());
                if (productCategory != null)
                {
                    return productCategory.PostDelete() != null;
                }
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }

        public static bool ChangeCategoryState(int productCategoryId)
        {
            try
            {
                var productCategory = GetProductCategoryById(productCategoryId);
                if (productCategory != null)
                {
                    productCategory.IsActive = productCategory.IsActive == true ? false : true;
                    return productCategory.PostModify() != null;
                }
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }

        /// <summary>
        /// 验证分类是否存在
        /// </summary>
        /// <param name="name">分类名</param>
        /// <returns>true:不存在 false:存在</returns>
        public static bool IsExits(string name)
        {
            if (new ProductCategory().Any(c => c.CategoryName.Equals(name)))
            {
                return false;
            }
            return true;
        }
    }
}
